//
//  ViewController.m
//  练习XMLAndDOM
//
//  Created by 苗钧豪 on 16/8/10.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property(nonatomic,strong)NSMutableArray *stuArr;
@property(nonatomic,copy)NSString *currentElementName;
@property(nonatomic,strong)NSMutableArray *stuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // XMLReader ———— github上下载
    // 将XML结构，转换成字典
}
// XML分为两种解析方式
// 第一种：SAX 逐行解析 ———— 使用系统的解析器 NSXMLParser
- (IBAction)SAX:(id)sender {
    // SAX解析
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmParser = [[NSXMLParser alloc]initWithData:data];
    xmParser.delegate = self;
    [xmParser parse];
}
// 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArr = [NSMutableArray array];
}
// 节点头调用的方法，记录当前节点，方便后续赋值
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"%@",elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *student = [Student new];
        [_stuArr addObject:student];
    }

}
// 节点的值，根据节点决定值，没有值时对应的结果： \n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSLog(@"%@",string);
    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
        Student *student = [_stuArr lastObject];
        [student setValue:string forKey:_currentElementName];
    }
}
// 节点尾调用的方法（方法的回调），判断当前节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"%@",elementName);
}
// 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"~~~~~~~~~~~~~~~~~~~~~%@",_stuArr);
}
// 解析失败，调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败");
}


// 第二种：DOM
// 关于 GDataXMLNode 的引入
/*
 引入.h 与.m 文件
 引入系统动态库libxml2.tbd
 根据 GDataXMLNode 头文件的提示，添加 /usr/include/libxml2
 如果是ARC环境，需对该文件进行MRC编译，添加 -fno-objc-arc
 */
- (IBAction)DOM:(id)sender {
    self.stuArray = [NSMutableArray array];
    // DOM解析
    NSString *xmlPath = [[NSBundle mainBundle]pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
    // 创建GDataXMLDocument对象
    // 根据XML的Data数据生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error];
    // 根据 Document 获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    NSLog(@"%@",rootElement.name);
    // 根据根节点获取子节点数组
    NSArray *rootChildElement = rootElement.children;
    // 遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElement) {
        NSLog(@"%@",childElement.name);
        // 对应所有student节点
        Student *stu = [Student new];
        [self.stuArray addObject:stu];
        // 遍历 student 节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            NSLog(@"%@",grandSonElement.stringValue);
            // 赋值
            [stu setValue:grandSonElement forKey:grandSonElement.stringValue];
        }
    }
    NSLog(@"%@",self.stuArray);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
